<?php

/**
 * Behavior para ajustar o formato de data
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource
 * @author        Juan Basso <jrbasso@gmail.com>
 * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
 */

/**
 * AjusteDataBehavior
 *
 * @link http://wiki.github.com/jrbasso/cake_ptbr/behavior-ajustedata
 */
class AjusteDocumentosBehavior extends ModelBehavior {

    /**
     * Configuração dos campos
     *
     * @var array
     * @access public
     */
    public $campos;

    /**
     * Setup
     *
     * @param object $model
     * @param array $config
     * @return void
     * @access public
     */
    public function setup(Model $model, $config = array()) {
        if (empty($config)) {
            // Caso n�o seja informado os campos, ele ir� buscar no schema
            $this->campos[$model->name] = $this->_buscaCamposDocumentos($model);
        } elseif (!is_array($config)) {
            $this->campos[$model->name] = array($config);
        } else {
            $this->campos[$model->name] = $config;
        }
    }

    /**
     * Before Save
     *
     * @param object $model
     * @return boolean
     * @access public
     */
    public function beforeSave(Model $model, $options = array()) {
        return $this->ajustarDocumentos($model);
    }

    /**
     * Before Save
     *
     * @param object $model
     * @return boolean
     * @access public
     */
    public function afterFind(Model $model, $results, $primary = false) {
        if (isset($results[$model->name]['documento'])) {
            $results[$model->name]['documento'] = $this->_mascara($results[$model->name]['documento']);
        } else if (count($results) > 0) {
            foreach ($results as $key => $value) {
                $results[$key][$model->name]['documento'] = $this->_mascara($results[$key][$model->name]['documento']);
            }
        }
        return parent::afterFind($model, $results, $primary);
    }

    public function _mascara($str) {
        if (strlen($str) == 11) {
            $str = substr($str, 0, 3) . '.' . substr($str, 3, 3) . '.' . substr($str, 6, 3) . '-' . substr($str, 9, 2);
        } else if (strlen($str) == 14) {
            $str = substr($str, 0, 2) . '.' . substr($str, 2, 3) . '.' . substr($str, 5, 3) . '/' . substr($str, 8, 4) . '-' . substr($str, 12, 2);
        }
        return $str;
    }

    /**
     * Corrigir as datas
     *
     * @param object $model
     * @return boolean
     * @access public
     */
    public function ajustarDocumentos(Model $model) {
        $data = & $model->data[$model->name];
        if (count($this->campos[$model->name]) > 0) {
            $data[$this->campos[$model->name][0]] = trim(str_replace(array('-', '.', '/', ' '), '', $data[$this->campos[$model->name][0]]));
        }
        return true;
    }

    /**
     * Buscar campos de data nos dados da model
     *
     * @param object $model
     * @return array Lista dos campos
     * @access protected
     */
    public function _buscaCamposDocumentos(Model &$model) {
        $schema = $model->schema();
        if (!is_array($schema)) {
            return array();
        }
        $saida = array();
        if (isset($schema['documento'])) {
            $saida[] = 'documento';
        }
        return $saida;
    }

}
